package hu.kami.twobears;

import java.io.InputStream;

import org.armedbear.lisp.Keyword;
import org.armedbear.lisp.Lisp;
import org.armedbear.lisp.LispObject;
import org.armedbear.lisp.Stream;
import org.armedbear.lisp.Symbol;

public class Utils {
	/**
	 * @see http://abcl.org/trac/browser/trunk/abcl/src/org/armedbear/lisp/scripting/AbclScriptEngine.java
	 * @param classpathResource
	 * @return
	 */
	public static LispObject loadFromClasspath(String classpathResource) {
		InputStream istream = Utils.class.getResourceAsStream(classpathResource);
		Stream stream = new Stream(Symbol.SYSTEM_STREAM, istream, Symbol.CHARACTER);
		return load(stream);
	}

	/**
	 * @see http://abcl.org/trac/browser/trunk/abcl/src/org/armedbear/lisp/scripting/AbclScriptEngine.java
	 * @param stream
	 * @return
	 */
	private static LispObject load(Stream stream) {
		Symbol keyword_verbose = Lisp.internKeyword("VERBOSE");
		Symbol keyword_print = Lisp.internKeyword("PRINT");
		return Symbol.LOAD.getSymbolFunction().execute
				(new LispObject[] { stream, keyword_verbose, Lisp.NIL,
						keyword_print, Lisp.NIL,
						Keyword.IF_DOES_NOT_EXIST, Lisp.T,
						Keyword.EXTERNAL_FORMAT, Keyword.DEFAULT });
	}
}
